package com.irdstudio.efp.esb.api.rest.sed.customer;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.bean.sed.customer.req.ReqSedCustomerCollectionBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.service.sed.customer.NlsCustomerProcessService;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
* 普税贷产品对税e贷产品的此部分代码做了复用
* 说明：为了保证功能的复用性、提升开发的工作效率，对该代码进行了复用
* 评估是否会影响线上功能：否
* 评估时间：2020/5/25
* 处理方案：保留复用
*/
/**
 * 需求编号:2019D0519
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/7/17
 * 功能描述:客户信息补录接口
 * 修改日期:2019/7/17
 * 修改描述:
 */
@RestController
@RequestMapping("/esb")
public class CustomerController implements EsbController {
    /** 日志信息*/
    private final Logger logger = LoggerFactory.getLogger(CustomerController.class);
    /** 日志模板*/
    private final String msgTemplate = "message{}";

    @Autowired
    private NlsCustomerProcessService nlsCustomerProcessService;

    @PostMapping("/SERVICE_20120001_06")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @Validated({GroupeInterface.psd.class}) @ModelAttribute("reqSedCustomerCollectionBean") ReqSedCustomerCollectionBean reqSedCustomerCollectionBean, BindingResult bindingResult2, Exception exception) throws Exception {
        try {
            // 参数校验
            validateBean(esbReqServiceBean, bindingResult1, reqSedCustomerCollectionBean, bindingResult2, exception,logger);


            // 授信申请流水号
            String crdtAppFlowNo = reqSedCustomerCollectionBean.getLmtApplySeq();

            // 全局关联流水号
            String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
            logger.info("授信申请流水号为："+crdtAppFlowNo+" 全局流水号为："+glblSrlNo+" 开始进行客户信息补录");
            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);

            // 执行客户补录流程
            return nlsCustomerProcessService.processCustomerCollection(esbReqServiceBean,reqSedCustomerCollectionBean);
        } catch (ESBException e) {
            return  EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean,e.getErrorCode(),e.getMessage());
        }
    }


    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    @ModelAttribute("reqSedCustomerCollectionBean")
    public ReqSedCustomerCollectionBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, ReqSedCustomerCollectionBean.class);
    }

}
